package com.jdb.settings.controller;

import com.alibaba.fastjson.JSON;
import com.guns21.jackjson.annotation.JsonMixin;
import com.guns21.jackjson.annotation.JsonResponse;
import com.guns21.jackjson.mixins.ResultMixin;
import com.guns21.result.domain.PageResult;
import com.guns21.result.domain.Result;
import com.guns21.support.controller.BaseController;
import com.guns21.support.util.WebUtils;
import com.jdb.settings.api.SettingDTO;
import com.jdb.settings.controller.mixins.HomeCategoryMixin;
import com.jdb.settings.privoder.service.SettingsQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author jliu
 */
@RestController
@RequestMapping("/api/home/v1")
public class HomeLoanCategoryController extends BaseController {

    @Autowired
    private SettingsQueryService settingsQueryService;

    /**
     * 首页贷款分类
     *
     * @return
     */
    @GetMapping("/categories")
    @JsonResponse(mixins = {@JsonMixin(target = PageResult.class, mixin = ResultMixin.class),
            @JsonMixin(target = Map.class, mixin = HomeCategoryMixin.class)})
    public Result categories() {
        Result<SettingDTO> homeCategory = settingsQueryService.get("homeCategory");
        if (homeCategory.getSuccess() && Objects.nonNull(homeCategory.getData())) {
            List<Map<String, String>> options = homeCategory.getData().getOptions();
            options.stream().forEach(o->{
                if (StringUtils.isNoneEmpty(o.get("cover"))) {
                    o.put("cover", WebUtils.buildWebUrlWithFile(o.get("cover")));
                }
            });

            return Result.success(options);
        }
        return Result.success();
    }
}
